package edu.usm.cs.roberge.workers;

import javax.swing.SwingWorker;

import edu.usm.cs.roberge.Message;
import edu.usm.cs.roberge.NewsReaderImpl;
import edu.usm.cs.roberge.NewsReaderImpl.NewsReaderException;

/**
 * Retrieves the message body for a particular {@link Message}
 * @author Dave
 *
 */
public class FetchMessageBodyWorker extends SwingWorker<Message, Void> {
	
	private final Message targetMessage;
	private final NewsReaderImpl impl;

	public FetchMessageBodyWorker(NewsReaderImpl impl, Message targetMessage) {
		this.impl = impl;
		this.targetMessage = targetMessage;
	}

	@Override
	protected Message doInBackground() throws Exception {
		if (targetMessage.hasBody()) {
			return targetMessage;
		}
		
		if (!impl.getArticleBody(targetMessage)) {
			throw new NewsReaderException("No body found for message:" + targetMessage.toString());
		}
		
		return targetMessage;
	}

}
